package android.slc.box.core.utils.interference;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

public class HandlerInterferenceManager<HI extends OnHandlerInterference<I>,I> {
    private final List<HI> onHandlerInterferenceList = new ArrayList<>();

    public final void add(HI onHandlerInterference) {
        if (!onHandlerInterferenceList.contains(onHandlerInterference)) {
            onHandlerInterferenceList.add(onHandlerInterference);
        }
    }

    public final void remove(HI onHandlerInterference) {
        onHandlerInterferenceList.remove(onHandlerInterference);
    }

    public void sort() {
        onHandlerInterferenceList.sort(Comparator.comparingInt(OnHandlerInterference::getPriority));
    }

    public List<HI> getOnHandlerInterferenceList() {
        sort();
        return onHandlerInterferenceList;
    }

    public Iterator<HI> iterator() {
        return getOnHandlerInterferenceList().iterator();
    }

    public void iterator(Consumer<HI> action) {
        Iterator<HI> onHandlerInterferenceIterator = iterator();
        while (onHandlerInterferenceIterator.hasNext()) {
            HI onHandlerInterference = onHandlerInterferenceIterator.next();
            action.accept(onHandlerInterference);
        }
    }
}
